package util;

import android.text.TextUtils;
import android.widget.FrameLayout;

import java.io.File;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.CellFormat;
import jxl.format.Format;

/**
 * 创建日期：18-9-30 下午2:06
 *
 * @author machong
 * @company 文件名称： ExcelTools.java
 * 类说明：对Execl文件做数据处理
 */
public class ExcelTools {
    private static final String TAG = "ExcelTools";

    private Workbook book;

    public void ExcelTools() {
        book = null;
    }

    private static ExcelTools mExcelTools;

    public static ExcelTools getInstance() {
        if (mExcelTools == null) {
            mExcelTools = new ExcelTools();
        }
        return mExcelTools;
    }

    /**
     * 读取Excel文件生成对象
     *
     * @param file_path
     * @return
     */
    public boolean openExcel(String file_path) {
        boolean b_ret = TextUtils.isEmpty(file_path);
        if (b_ret)
            LogUtil.d(TAG, "ExcelTools: file is empty");
        try {
            File m_file = new File(file_path);
            if (m_file.isFile()) {
                book = Workbook.getWorkbook(m_file);
                return true;
            } else {
                LogUtil.e(TAG, "openExcel: file error");
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.e(TAG, "openExcel: e = " + e.getMessage());
        }
        return false;
    }

    /**
     * 读取指定sheet表单
     *
     * @param m_index sheet下标
     * @return
     */
    public Sheet getExcelSheet(int m_index) {
        Sheet m_sheet;
        try {
            if (book != null) {
                int num = book.getNumberOfSheets();
                boolean b = m_index >= 0 && m_index < num ? true : false;
                if (b) {
                    m_sheet = book.getSheet(m_index);
                } else
                    m_sheet = null;
            } else
                m_sheet = null;
        } catch (IndexOutOfBoundsException e) {
            LogUtil.e(TAG, "getExcelSheet: e = " + e.getMessage());
            m_sheet = null;
        }

        return m_sheet;
    }

    public String getExcelCell(Sheet m_sheet, int m_col, int m_row) {
        Cell m_cell = m_sheet.getCell(m_col, m_row);
        return m_cell.getContents();
    }


    public Cell[] getListByCol(Sheet m_sheet, int m_col) {
        return m_sheet.getColumn(m_col);
    }
}
